package com.dianrui.agent.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.dianrui.agent.Constant;
import com.dianrui.agent.R;
import com.dianrui.agent.util.SPUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.util.List;


public class SplashActivity extends Activity {
    public SPUtils spUtils;
    boolean isFirstIn;
    SharedPreferences firstInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        firstInfos=getSharedPreferences("FirstInfo",MODE_PRIVATE);
        isFirstIn =firstInfos.getBoolean("isFirstIn",true);
        //隐藏底部的虚拟按钮
        final View decorView = getWindow().getDecorView();
        final int uiOption = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOption);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isFirstIn) {
                    showUserProtocolDialog();//第一次启动出现隐私对话框，下次启动没必要显示了
                }else {
                    readyGoThenKill(LoginActivity.class);
                }

            }
        }, 1000);
        requestPermission();
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void requestPermission() {
        AndPermission.with(this)
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_SMS,
                        Manifest.permission.RECEIVE_SMS,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.REQUEST_INSTALL_PACKAGES,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.ACCESS_FINE_LOCATION).onDenied(new Action() {
            @Override
            public void onAction(List<String> permissions) {

            }
        }).onDenied(new Action() {
            @Override
            public void onAction(List<String> permissions) {

            }
        }).start();

    }



    //首次启动显示用户协议和隐私对话框
    private void showUserProtocolDialog() {
        final Dialog dialog = new Dialog(SplashActivity.this, R.style.RcDialog);
        View view = LayoutInflater.from(SplashActivity.this).inflate(R.layout.user_protocol_ask, null);
        TextView contentDialogAsk =view.findViewById(R.id.content_dialog_ask);
        final TextView cancelDialogAsk = view.findViewById(R.id.cancel_dialog_ask);
        final TextView yesDialogAsk = view.findViewById(R.id.yes_dialog_ask);

        SpannableStringBuilder style=new SpannableStringBuilder(getString(R.string.dialog_protocol_txt));
        style.setSpan(new TextClick(),115,122 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        style.setSpan(new TextClick1(),124,131 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        style.setSpan(new ForegroundColorSpan(ContextCompat.getColor(SplashActivity.this,R.color.black)),115,122, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(ContextCompat.getColor(SplashActivity.this,R.color.black)),124,131, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        contentDialogAsk.setText(style);
        contentDialogAsk.setHighlightColor(getResources().getColor(android.R.color.transparent));
        contentDialogAsk.setMovementMethod(LinkMovementMethod.getInstance());


        cancelDialogAsk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        yesDialogAsk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                readyGoThenKill(LoginActivity.class);
            }
        });
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }


    public class TextClick extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            if(widget  instanceof TextView){
                ((TextView)widget).setHighlightColor(getResources().getColor(android.R.color.transparent));
            }
            startActivity(new Intent(SplashActivity.this, WebViewActivity.class).putExtra("type", "useagree"));
        }
        @Override
        public void updateDrawState(TextPaint ds) {

        }
    }

    public class TextClick1 extends ClickableSpan{
        @Override
        public void onClick(View widget) {
            if(widget  instanceof  TextView){
                ((TextView)widget).setHighlightColor(getResources().getColor(android.R.color.transparent));
            }
            startActivity(new Intent(SplashActivity.this, WebViewActivity.class).putExtra("type", "secret"));
        }
        @Override
        public void updateDrawState(TextPaint ds) {

        }
    }

    public  void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }
}
